const BloomFilter = require("../template")

function testBloomFilterPerformance() {
  console.log(
    "1. 添加性能测试:",
    (() => {
      const bf = new BloomFilter(10000, 0.01)
      const iterations = 5000
      
      const start = performance.now()
      
      for (let i = 0; i < iterations; i++) {
        bf.add(`item${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return bf.getItemCount() === iterations && 
             duration < 1000 && // 应该在1秒内完成
             duration > 0
    })()
  )

  console.log(
    "2. 查询性能测试:",
    (() => {
      const bf = new BloomFilter(10000, 0.01)
      const iterations = 5000
      
      // 先添加数据
      for (let i = 0; i < iterations; i++) {
        bf.add(`item${i}`)
      }
      
      const start = performance.now()
      
      // 查询所有数据
      for (let i = 0; i < iterations; i++) {
        bf.contains(`item${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 500 && // 查询应该很快
             duration > 0
    })()
  )

  console.log(
    "3. 批量操作性能测试:",
    (() => {
      const bf = new BloomFilter(5000, 0.01)
      const batchSize = 1000
      
      const start = performance.now()
      
      // 批量添加
      for (let i = 0; i < batchSize; i++) {
        bf.add(`batch_item${i}`)
      }
      
      // 批量查询
      for (let i = 0; i < batchSize; i++) {
        bf.contains(`batch_item${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return bf.getItemCount() === batchSize &&
             duration < 200 &&
             duration > 0
    })()
  )

  console.log(
    "4. 内存使用效率测试:",
    (() => {
      const bf = new BloomFilter(1000, 0.01)
      
      // 添加数据直到接近容量
      for (let i = 0; i < 800; i++) {
        bf.add(`memory_item${i}`)
      }
      
      const bitArraySize = bf.getBitArraySize()
      const hashFunctionCount = bf.getHashFunctionCount()
      const density = bf.getBitArrayDensity()
      
      return bitArraySize > 0 &&
             hashFunctionCount > 0 &&
             density >= 0 && density <= 1 &&
             bf.getItemCount() === 800
    })()
  )

  console.log(
    "5. 哈希函数性能测试:",
    (() => {
      const bf = new BloomFilter(1000, 0.01)
      const testString = "performance_test_string"
      
      const start = performance.now()
      
      // 测试哈希函数性能
      const hashFunctions = bf.getHashFunctions()
      for (let i = 0; i < 1000; i++) {
        hashFunctions.forEach(hashFunc => hashFunc(testString + i))
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 100 && // 哈希函数应该很快
             duration > 0 &&
             hashFunctions.length > 0
    })()
  )

  console.log(
    "6. 并发操作模拟测试:",
    (() => {
      const bf = new BloomFilter(5000, 0.01)
      const operations = 1000
      
      const start = performance.now()
      
      // 模拟并发操作：交替添加和查询
      for (let i = 0; i < operations; i++) {
        if (i % 2 === 0) {
          bf.add(`concurrent_item${i}`)
        } else {
          bf.contains(`concurrent_item${i - 1}`)
        }
      }
      
      const end = performance.now()
      const duration = end - start
      
      return bf.getItemCount() === operations / 2 &&
             duration < 300 &&
             duration > 0
    })()
  )

  console.log(
    "7. 大数据量性能测试:",
    (() => {
      const bf = new BloomFilter(50000, 0.01)
      const largeSize = 20000
      
      const start = performance.now()
      
      // 添加大量数据
      for (let i = 0; i < largeSize; i++) {
        bf.add(`large_item${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      // 验证数据完整性
      let allCorrect = true
      for (let i = 0; i < Math.min(1000, largeSize); i++) { // 抽样检查
        if (!bf.contains(`large_item${i}`)) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect &&
             bf.getItemCount() === largeSize &&
             duration < 2000 && // 大数据量应该在2秒内完成
             duration > 0
    })()
  )

  console.log(
    "8. 假阳性率计算性能测试:",
    (() => {
      const bf = new BloomFilter(10000, 0.01)
      
      // 添加数据
      for (let i = 0; i < 5000; i++) {
        bf.add(`rate_item${i}`)
      }
      
      const start = performance.now()
      
      // 多次计算假阳性率
      for (let i = 0; i < 100; i++) {
        bf.getFalsePositiveRate()
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 100 && // 计算应该很快
             duration > 0
    })()
  )

  console.log(
    "9. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(size) {
          const bf = new BloomFilter(size, 0.01)
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            bf.add(`perf_item${i}`)
          }
          
          const end = performance.now()
          return (end - start) * this.multiplier
        }
      }
      
      const result = perfObj.processPerformance(1000)
      return result > 0 && result < 1000
    })()
  )

  console.log(
    "10. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { threshold: 1000 },
        processWithConfig: function(size) {
          const bf = new BloomFilter(size, 0.01)
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            bf.add(`config_item${i}`)
          }
          
          const end = performance.now()
          return (end - start) < this.config.threshold
        }
      }
      
      return perfConfigObj.processWithConfig(2000) === true
    })()
  )

  console.log(
    "11. 性能一致性测试:",
    (() => {
      const bf = new BloomFilter(5000, 0.01)
      const iterations = 1000
      
      const start1 = performance.now()
      for (let i = 0; i < iterations; i++) {
        bf.add(`consistency_item${i}`)
      }
      const end1 = performance.now()
      
      const start2 = performance.now()
      for (let i = 0; i < iterations; i++) {
        bf.contains(`consistency_item${i}`)
      }
      const end2 = performance.now()
      
      const addTime = end1 - start1
      const searchTime = end2 - start2
      
      return addTime > 0 &&
             searchTime > 0 &&
             searchTime < addTime && // 查询应该比添加快
             bf.getItemCount() === iterations
    })()
  )

  console.log(
    "12. 极端性能测试:",
    (() => {
      const extremeBf = new BloomFilter(1, 0.01) // 最小位数组
      const iterations = 1000
      
      const start = performance.now()
      
      // 在极端条件下添加数据
      for (let i = 0; i < iterations; i++) {
        extremeBf.add(`extreme_item${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      // 验证数据完整性
      let allCorrect = true
      for (let i = 0; i < iterations; i++) {
        if (!bf.contains(`extreme_item${i}`)) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect &&
             extremeBf.getItemCount() === iterations &&
             duration < 1000 && // 即使在极端条件下也应该在合理时间内完成
             duration > 0
    })()
  )
}

testBloomFilterPerformance()
